package com.unlcn.ils.erpInterfaces.service.buss.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.unlcn.ils.erpInterfaces.dao.mapper.SoorderMapper;
import com.unlcn.ils.erpInterfaces.dao.mapper.WmsOrderLogMapper;
import com.unlcn.ils.erpInterfaces.dao.model.*;
import com.unlcn.ils.erpInterfaces.dao.model.bo.WmsOrderLogBo;
import com.unlcn.ils.erpInterfaces.service.buss.*;
import com.unlcn.ils.erpInterfaces.service.util.BaseException;
import com.unlcn.ils.erpInterfaces.service.util.UniquenessFlagUnits;
import com.unlcn.ils.erpInterfaces.service.util.enums.CustomerIdEnum;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * $ 订单接口日志表服务实现类
 * </p>
 *
 * @author liangpeng
 * @since 2018-06-06
 */
@Service
public class WmsOrderLogServiceImpl extends ServiceImpl<WmsOrderLogMapper, WmsOrderLog> implements IWmsOrderLogService {

    @Autowired
    private SoorderMapper soorderMapper;

    @Autowired
    private WmsOrderLogMapper wmsOrderLogMapper;

    @Autowired
    private IWmsOrderExceptionService wmsOrderExceptionService;

    @Autowired
    private ISmcitymapService smcitymapService;

    @Autowired
    private ISmcustService smcustService;

    @Autowired
    private ISmcustomerService smcustomerService;

    @Autowired
    private ISmcontractService smcontractService;

    @Autowired
    private ISmwarehouseService smwarehouseService;

    @Autowired
    private ISmstyleService smstyleService;

    @Autowired
    private ISoorderService soorderService;

    private static final Logger LOGGER = LoggerFactory.getLogger(IWmsOrderLogService.class);

    @Override
    public Long getNextIlineid() {
        return wmsOrderLogMapper.getNextIlineid();
    }

    @Transactional(propagation= Propagation.REQUIRED)
    @Override
    public String saveOrderByWms(WmsOrderLogBo wmsOrderLogBo) throws BaseException {

        // 检查数据
        checkData(wmsOrderLogBo);

        // 拷贝属性
        WmsOrderLog wmsOrderLog = new WmsOrderLog();
        BeanUtils.copyProperties(wmsOrderLogBo,wmsOrderLog);

        // 先判断是否存在
        EntityWrapper<WmsOrderLog> ew = new EntityWrapper<>();
        ew.eq("HANDOVER_NUMBER", wmsOrderLogBo.getHandoverNumber());
        ew.eq("VIN", wmsOrderLogBo.getVin());
        List<WmsOrderLog> lists =  wmsOrderLogMapper.selectList(ew);
        if(CollectionUtils.isEmpty(lists)){
            // 不存在，则插入
            Long id = wmsOrderLogMapper.getNextIlineid();
            wmsOrderLog.setId(id);
            wmsOrderLog.setGmtCreate(new Date());
            wmsOrderLogMapper.insert(wmsOrderLog);
        } else{
            // 存在，则更新
            wmsOrderLog.setId(lists.get(0).getId());
            wmsOrderLog.setGmtModify(new Date());
            this.updateById(wmsOrderLog);
        }

        // 获取起运城市、目的城市信息
        Smcity startCity = smcitymapService.judgeCityMap(wmsOrderLog.getDepartCityName());
        Smcity endCity = smcitymapService.judgeCityMap(wmsOrderLog.getReceiptCityName());

        if (startCity == null)
            throw new BaseException("获取起运地信息失败");
        if (endCity == null)
            throw new BaseException("获取目的地信息失败");

        // 获取客户体系 id
        Long custId = smcustService.getCustId(wmsOrderLogBo.getCustomName());
        // 获取客户 id
        Long customerId = smcustomerService.getCustomerId(wmsOrderLogBo.getCustomName(), startCity.getIlineid(), custId);
        // 获取客户合同 id
        Long contractId = smcontractService.getContractId(customerId);
        // 获取装车点 id
        Long warehouseId = smwarehouseService.getSmwarehouseId(customerId,contractId,startCity);
        // 获取车型 id
        Long styleId = smstyleService.getStyleId(wmsOrderLogBo.getModelName(), contractId);

        // 保存订单信息
        Soorder soorder = new Soorder();
        soorder.setIcustomerid(Double.valueOf(customerId));    // 所属客户 id
        soorder.setIcontractid(Double.valueOf(contractId));    // 所属合同 id
        soorder.setIwarehouseid(Double.valueOf(warehouseId));  // 所属作业点 id
        soorder.setIstyleid(Double.valueOf(styleId));          // 车辆型号 id
        soorder.setVcstyleno(wmsOrderLog.getModelName());      // 车辆型号名称

        // 保存订单，并且获取系统订单号
        String vcorderNo = soorderService.saveOrder(wmsOrderLog, soorder, startCity, endCity);

        return vcorderNo;
    }

    @Transactional(propagation= Propagation.REQUIRES_NEW)
    @Override
    public void saveOrderException(WmsOrderLogBo wmsOrderLogBo, String ex) {

        if(!Objects.isNull(wmsOrderLogBo)){
            WmsOrderException wmsOrderException = new WmsOrderException();
            Long id = wmsOrderExceptionService.getNextIlineid();
            wmsOrderException.setId(id);
            wmsOrderException.setHandoverNumber(wmsOrderLogBo.getHandoverNumber());
            wmsOrderException.setVin(wmsOrderLogBo.getVin());
            wmsOrderException.setCustomid(wmsOrderLogBo.getCustomId());
            wmsOrderException.setCustomname(wmsOrderLogBo.getCustomName());
            wmsOrderException.setExceptionMsg(ex);
            wmsOrderException.setFlag(0);
            wmsOrderException.setGmtModify(new Date());
            wmsOrderException.setGmtCreate(new Date());
            wmsOrderExceptionService.insert(wmsOrderException);
        }
    }

    @Override
    public void checkData(WmsOrderLogBo wmsOrderLogBo) {

        // 判断是否为空
        if(Objects.isNull(wmsOrderLogBo)){
            throw new BaseException("传入的订单接口日志为空");
        }

        // 先判断相应必填的值是否存在
        if(StringUtils.isEmpty(wmsOrderLogBo.getHandoverNumber())){
            throw new BaseException("交接单号为空");
        }

        if(StringUtils.isEmpty(wmsOrderLogBo.getGroupBoardNo())){
            throw new BaseException("组板单号为空");
        }

        if(StringUtils.isEmpty(wmsOrderLogBo.getOrderno())){
            throw new BaseException("wms订单号为空");
        }

        if(StringUtils.isEmpty(wmsOrderLogBo.getCustomName())){
            throw new BaseException("客户名称为空");
        }

        if(StringUtils.isEmpty(wmsOrderLogBo.getOrderDate())){
            throw new BaseException("订单日期为空");
        }

//        if(StringUtils.isEmpty(wmsOrderLogBo.getCreator())){
//            throw new BaseException("下单人为空");
//        }

        if(StringUtils.isEmpty(wmsOrderLogBo.getVin())){
            throw new BaseException("车架号为空");
        }

        if(StringUtils.isEmpty(wmsOrderLogBo.getModelId())){
            throw new BaseException("车型代码为空");
        }

        if(StringUtils.isEmpty(wmsOrderLogBo.getModelName())){
            throw new BaseException("车型名称为空");
        }

        if(StringUtils.isEmpty(wmsOrderLogBo.getModelDesc())){
            throw new BaseException("车型详细说明为空");
        }

        if(StringUtils.isEmpty(wmsOrderLogBo.getDepartCityName())){
            throw new BaseException("发车市名称为空");
        }

        if(StringUtils.isEmpty(wmsOrderLogBo.getDepartAddr())){
            throw new BaseException("发车详细地址为空");
        }

        if(StringUtils.isEmpty(wmsOrderLogBo.getDepartContact())){
            throw new BaseException("发车联系人为空");
        }

        if(StringUtils.isEmpty(wmsOrderLogBo.getDepartPhone())){
            throw new BaseException("发车联系电话为空");
        }

        if(StringUtils.isEmpty(wmsOrderLogBo.getReceiptCityName())){
            throw new BaseException("收车市名称为空");
        }

        if(StringUtils.isEmpty(wmsOrderLogBo.getReceiptAddr())){
            throw new BaseException("收获地址为空");
        }

        if(StringUtils.isEmpty(wmsOrderLogBo.getReceiptContact())){
            throw new BaseException("收货联系人为空");
        }

        if(StringUtils.isEmpty(wmsOrderLogBo.getReceiptPhone())){
            throw new BaseException("收货联系电话为空");
        }

        if(wmsOrderLogBo.getCount() == null){
            throw new BaseException("数量为空");
        }

    }
}
